#include "SHT30.h"

static struct bflb_device_s *i2c;
static struct bflb_i2c_msg_s msgs[2];
/* SHT30指令发送参数值 */
uint8_t SHT30CMDSendData[SHT30_CMD_LAST][SHT30_CMD_LEN_BYTE] = 
{
	{0x2C, 0x06},
	{0x2C, 0x0D},
	{0x2C, 0x10},
	{0x24, 0x00},
	{0x24, 0x0B},
	{0x24, 0x16},
	{0x20, 0x32},
	{0x20, 0x24},
	{0x20, 0x2F},
	{0x21, 0x30},
	{0x21, 0x26},
	{0x21, 0x2D},
	{0x22, 0x36},
	{0x22, 0x20},
	{0x22, 0x2B},
	{0x23, 0x34},
	{0x23, 0x22},
	{0x23, 0x29},
	{0x27, 0x37},
	{0x27, 0x21},
	{0x27, 0x2A},
	{0xE0, 0x00},   /* 周期模式的读出测量 */
	{0x2B, 0x32},   /* ART（加速响应） */
	{0x30, 0x93},   /* 中断指令或采集数据 */
	{0x30, 0xA2},   /* 软复位 */
	{0x00, 0x06},   /* 硬复位 */
	{0x30, 0x6D},   /* 使能加热器 */
	{0x30, 0x66},   /* 关闭加热器 */
	{0xF3, 0x2D},   /* 读取状态 */
	{0x30, 0x41},   /* 清除状态 */
};
/**************************Local Function**************************/
void IIC0_Init(void)
{
	struct bflb_device_s* gpio;
	
	gpio = bflb_device_get_by_name("gpio");
	/* I2C0_SCL */
	bflb_gpio_init(gpio, GPIO_PIN_0, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
	/* I2C0_SDA */
	bflb_gpio_init(gpio, GPIO_PIN_1, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
    
	/* IIC初始化，速度为400K */
	i2c = bflb_device_get_by_name("i2c0");
	bflb_i2c_init(i2c, 400000);
	
    //printf("IIC0 Init over\r\n");
}
void IIC_SenCmd(uint8_t _Cmd, uint8_t *Sendbuf, uint16_t _length)
{
	if (SHT30_ADDR)
	{
		msgs[0].addr = SHT30_ADDR_H;
	}
	else
	{
		msgs[0].addr = SHT30_ADDR_L;
	}
    msgs[0].flags = I2C_M_NOSTOP;
    msgs[0].buffer = &_Cmd;
    msgs[0].length = 1;

	if (_length != 0)
	{
		/* 有数据的发送 */
		if (SHT30_ADDR)
		{
			msgs[0].addr = SHT30_ADDR_H;
		}
		else
		{
			msgs[0].addr = SHT30_ADDR_L;
		}
     	msgs[1].flags = 0;
     	msgs[1].buffer = Sendbuf;
     	msgs[1].length = _length;
     	bflb_i2c_transfer(i2c, msgs, 2);
	}
	else
	{
		/* 只发一个字节的数据 */
		msgs[0].flags = 0;
		bflb_i2c_transfer(i2c, msgs, 1);
	}
}
void IIC_RecData(uint8_t *_Recbuf, uint16_t _length)
{
   	if (SHT30_ADDR)
	{
		msgs[0].addr = SHT30_ADDR_H;
	}
	else
	{
		msgs[0].addr = SHT30_ADDR_L;
	}
    msgs[0].flags = I2C_M_READ;
    msgs[0].buffer = _Recbuf;
    msgs[0].length = _length;
    bflb_i2c_transfer(i2c, msgs, 1);
}

SHT30CMDSendResu SHT30_SendTestMode(SHT30CMD _Cmd)
{
	if (_Cmd >= SHT30_CMD_LAST)
	{
		return SHT30_CMD_SEND_RESU_ERR;
	}
	IIC_SenCmd(SHT30CMDSendData[_Cmd][0], &SHT30CMDSendData[_Cmd][1], SHT30_CMD_LEN_BYTE - 1);

	//printf("Chip Test Mode CMD send over\r\n");
	return SHT30_CMD_SEND_RESU_OK;
}

/**************************Global Function**************************/
void SHT30_Init()
{
	/* IIC初始化 */
	IIC0_Init();
}

SHT30ReadResu SHT30_ReadTempAndHumi(float *_LuxVal)
{
	uint8_t RecData[7] = {0};
	uint16_t Temp = 0;

	if (SHT30_TEST_MODE > SHT30_CMD_MEAS_PERI_10_L)
	{
		return SHT30_READ_ERR;
	}
	if (SHT30_CMD_SEND_RESU_ERR == SHT30_SendTestMode(SHT30_TEST_MODE))
	{
		return SHT30_READ_ERR;
	}

	/* 等待测量完成 */
	bflb_mtimer_delay_ms(50);
	/* 接收测量数据 */
	IIC_RecData(RecData, 6);

	Temp = (RecData[0] << 8) | RecData[1];
	*(_LuxVal++) = (float)Temp * 175.0 / 65535.0 -45.0;
	Temp = (RecData[3] << 8) | RecData[4];
	*_LuxVal = (float)Temp * 100.0 / 65535.0;

	return SHT30_READ_OK;
}
